package me.helllp.book.netty.step08;

import com.google.protobuf.InvalidProtocolBufferException;

public class TestProtobuf {
	private static byte[] encode(SubscribeReqProto.SubscribeReq req){
		return req.toByteArray();
	}
	
	private static SubscribeReqProto.SubscribeReq decode(byte[] bytes) throws InvalidProtocolBufferException{
		return SubscribeReqProto.SubscribeReq.parseFrom(bytes);
	}
	
	private static SubscribeReqProto.SubscribeReq create(){
		SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
		builder.setAddress("地址");
		builder.setPhoneNumber("电话号码");
		builder.setProductName("作品名称");
		builder.setSubReqId(12).setUserName("用户名");
		
		return builder.build();
	}
	
	public static void main(String[] args) throws InvalidProtocolBufferException {
		SubscribeReqProto.SubscribeReq req = create();
		
		byte[] bytes = encode(req);
		System.out.println(bytes.length);
		
		SubscribeReqProto.SubscribeReq out = decode(bytes);
		
		System.out.println(out);
	}
}
